<?php
namespace app\admin\controller\rider;
use think\Db;
class Rider extends \app\admin\controller\Common
{
    public function index(){
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\Rider())
                ->field([
                    'id','username','avatar','nickname','is_state','status'
                ])->append(['is_state_text','status_text']);

        isset($search['username']) && $search['username'] != "" ? $query->where("username like '%{$search['username']}%'") : '';

        $query->order("id desc");
        $list = $query->limit($page, $limit)->select();
        $count = $query->count();
   
        return self::successResult('',$list,$count);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
     
            $validate = new \app\common\validate\Rider();
            $result = $validate->scene('add')->check($data);
            if ($result === false) {
                self::errorResult($validate->getError());
            }
            if(isset($data['status'])){
                $data['status'] = 2;
            }
            if(isset($data['password'])){
                $data['password'] = StrMD5($data['password']);
            }
            (new \app\common\model\Rider())->save($data);

            return self::successResult();
        } else {
            $this->assign("type_arr",(new \app\common\model\Version())->tpye_arr());
            return view();
        }
    }
    
    public function edit()
    {
        $id = request()->param('id');
        if (request()->isPost()) {
            $data = request()->param('info');
            $validate = new \app\common\validate\Rider();
            $result = $validate->scene('pass')->check($data);
            if ($result === false) {
                self::errorResult($validate->getError());
            }
            if(isset($data['status'])){
                $data['status'] = 2;
            }
            if(isset($data['password'])){
                $data['password'] = StrMD5($data['password']);
                unset($data['passwords']);
            }
            $row = (new \app\common\model\Rider())->where(['id' => $id])->update($data);
            return self::successResult();
        } else {
            $id = intval(request()->param('id'));
            $info = (new \app\common\model\Rider())->where(['id' => $id])->find();
            $this->assign('info',$info);
   
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\Rider();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
         return $row ? self::successResult("成功") : self::errorResult("失败");
    }
    /**
     * @description: 认证状态
     * @param {*} $value
     * @param {*} $data
     * @return {*}
     */  
    public function status(){
        $id = request()->param('id');
        $info = (new \app\common\model\Rider())->field([
                    'id','username','nickname','auth_name','auth_id','auth_positive_img','auth_back_img','fail_content','status'
                    ])
                ->where(['id' => $id])->find();
        if(empty($info['auth_name'])){
            return  "没有上传资料";
        }
        $this->assign('info',$info);
        return view();
    }
    public function certification(){
        $id = $this->request->param('id');
        $audit = $this->request->param('audit');
        $fail_content = $this->request->param('status_fail');
        if($audit == "yes"){
            $update = [
                'status' => 2,
                'updatetime' => time(),
            ];
        }
        if($audit == "no"){
            $update = [
                'status' => 3,
                'updatetime' => time(),
                'fail_content' => $fail_content,
            ];
        }
        (new \app\common\model\Rider())->where(['id' => $id])->update($update);
        return self::successResult();
    }
        /**
     * @description: 修改密码
     * @param {*} $value
     * @param {*} $data
     * @return {*}
     */  
    public function pass(){
        $id = request()->param('id');
        if (request()->isPost()) {
            $data = request()->param('info');
            $validate = new \app\common\validate\Rider();
            $result = $validate->scene('pass')->check($data);
            if ($result === false) {
                self::errorResult($validate->getError());
            }
            if(isset($data['password'])){
                $data['password'] = StrMD5($data['password']);
                unset($data['passwords']);
            }
            $row = (new \app\common\model\Rider())->where(['id' => $id])->update($data);
            return self::successResult();
        } else {
            $this->assign('id',$id);
            return view();
   
        }
    }
       /**
     * @description: 认证状态
     * @param {*} $value
     * @param {*} $data
     * @return {*}
     */  
    public function is_state(){
        $id = request()->param('id');
        if (request()->isPost()) {
            $is_state = request()->param('is_state');
            (new \app\common\model\Rider())->where(['id' => $id])->update([
                'is_state' => $is_state,
            ]);
            return self::successResult();
        }
    }


 
}
